package com.reading.core.utils;

import com.sun.org.apache.regexp.internal.RE;

import java.util.ArrayList;
import java.util.List;

/**
 * @author guwenqi_1995@163.com
 * @date 2019/7/11 11:52 AM
 **/
public class UUID {

    private static final char[] B = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G',
            'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

    /**
     * getUUID(参数1_参数2_参数3...)
     *
     * @param
     * @return
     */
    public static String getUUID(Integer... numbers) {
        StringBuffer uuid = new StringBuffer();
        for (int i = 0; i < numbers.length ; i++) {
            uuid.append(intToHex(numbers[i]));
            if (i < numbers.length - 1) {
                uuid.append("_");
            }
        }
        uuid.substring(uuid.length() - 1, uuid.length());
        return uuid.toString();
    }

    private static String intToHex(Integer number) {
        StringBuffer buffer = new StringBuffer();
        while(number != 0){
            buffer.append(B[number%B.length]);
            number = number/B.length;
        }
        return buffer.reverse().toString();
    }

    /**
     * 解码uuid
     *
     * @param uuid
     * @return
     */
    public static List<Integer> decodeUUID (String uuid) {
        String[] params = uuid.split("_");
        List<Integer> list = new ArrayList<>();
        for (String param : params) {
            list.add(Integer.parseUnsignedInt(param, B.length));
        }
        return list;
    }

    public static void main(String[] args) {

        String uuid = getUUID(233, 44, 33, 22);

        List<Integer> list = decodeUUID(uuid);

        System.out.println("uuid = " + uuid + ",list = " + list.toString());
    }
}
